/**
 * 
 */
package com.dalonedrau.jogl.engine;

import java.awt.Cursor;
import java.awt.GraphicsDevice;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCapabilitiesChooser;
import javax.media.opengl.GLContext;
import javax.media.opengl.awt.GLCanvas;

/**
 * The drawable canvas.
 * @author DaLoneDrau
 */
public abstract class GameDrawable extends GLCanvas {
	/** the id of the current cursor. */
	private static int			cursor					= Cursor.DEFAULT_CURSOR;
	/** the current cursor name. */
	private static String		cursorName;
	/** the default cursor. */
	private static Cursor		defaultCursor			= new Cursor(
																Cursor.DEFAULT_CURSOR);
	/** the hand cursor. */
	private static Cursor		handCursor				= new Cursor(
																Cursor.HAND_CURSOR);
	public static final int		PAN_EAST_CURSOR			= 23;
	public static final int		PAN_NORTH_CURSOR		= 21;
	public static final int		PAN_NORTHEAST_CURSOR	= 22;
	public static final int		PAN_NORTHWEST_CURSOR	= 20;
	public static final int		PAN_SOUTH_CURSOR		= 25;
	public static final int		PAN_SOUTHEAST_CURSOR	= 24;
	public static final int		PAN_SOUTHWEST_CURSOR	= 26;
	public static final int		PAN_WEST_CURSOR			= 27;
	public static final int		MOVE_CURSOR			= 28;
	public static final int		ATTACK_CURSOR			= 29;
	private static Cursor		panEast;
	private static Cursor		panNorth;
	private static Cursor		panNorthEast;
	private static Cursor		panNorthWest;
	private static Cursor		panSouth;
	private static Cursor		panSouthEast;
	private static Cursor		panSouthWest;
	private static Cursor		panWest;
	private static Cursor		move;
	private static Cursor		attack;
	/** the current screen. */
	private static GameDrawable	screen;
	/** serial id. */
	private static final long	serialVersionUID		= 1L;
	/** the wait cursor. */
	private static Cursor		waitCursor				= new Cursor(
																Cursor.WAIT_CURSOR);
	/**
	 * Sets the current cursor.
	 * @param newCursor the cursor id.
	 */
	public static void setCursor(final int newCursor) {
		GameDrawable.cursor = newCursor;
	}
	/**
	 * Sets a new {@link Cursor} for display.
	 * @param id the cursor id
	 * @param cursor the {@link Cursor} instance
	 */
	public static void setCursor(final int id, final Cursor cursor) {
		switch (id) {
		case Cursor.DEFAULT_CURSOR:
			GameDrawable.defaultCursor = cursor;
			break;
		case Cursor.HAND_CURSOR:
			GameDrawable.handCursor = cursor;
			break;
		case Cursor.WAIT_CURSOR:
			GameDrawable.waitCursor = cursor;
			break;
		case GameDrawable.PAN_EAST_CURSOR:
			GameDrawable.panEast = cursor;
			break;
		case GameDrawable.PAN_NORTH_CURSOR:
			GameDrawable.panNorth = cursor;
			break;
		case GameDrawable.PAN_NORTHEAST_CURSOR:
			GameDrawable.panNorthEast = cursor;
			break;
		case GameDrawable.PAN_NORTHWEST_CURSOR:
			GameDrawable.panNorthWest = cursor;
			break;
		case GameDrawable.PAN_SOUTH_CURSOR:
			GameDrawable.panSouth = cursor;
			break;
		case GameDrawable.PAN_SOUTHEAST_CURSOR:
			GameDrawable.panSouthEast = cursor;
			break;
		case GameDrawable.PAN_SOUTHWEST_CURSOR:
			GameDrawable.panSouthWest = cursor;
			break;
		case GameDrawable.PAN_WEST_CURSOR:
			GameDrawable.panWest = cursor;
			break;
		case GameDrawable.MOVE_CURSOR:
			GameDrawable.move = cursor;
			break;
		case GameDrawable.ATTACK_CURSOR:
			GameDrawable.attack = cursor;
			break;
		default:
			JOGLErrorHandler.getInstance().fatalError(
						new Exception("Invalid cursor type: " + id));
		}
	}
	public static void setPanCursor(final int id, final Image cursor) {
		switch (id) {
		case PAN_NORTHWEST_CURSOR:
			GameDrawable.panNorthWest = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan northwest");
			break;
		case PAN_NORTH_CURSOR:
			GameDrawable.panNorth = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan north");
			break;
		case PAN_NORTHEAST_CURSOR:
			GameDrawable.panNorthEast = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan northeast");
			break;
		case PAN_EAST_CURSOR:
			GameDrawable.panEast = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan east");
			break;
		case PAN_SOUTHEAST_CURSOR:
			GameDrawable.panSouthEast = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan southeast");
			break;
		case PAN_SOUTH_CURSOR:
			GameDrawable.panSouth = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan south");
			break;
		case PAN_SOUTHWEST_CURSOR:
			GameDrawable.panSouthWest = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan southwest");
			break;
		default:
			GameDrawable.panWest = Toolkit.getDefaultToolkit()
					.createCustomCursor(
							cursor, new Point(0, 0), "pan west");
			break;
		}
	}
	/** Switches the cursor displayed. */
	public static void switchCursor() {
		GameDrawable.cursorName = GameDrawable.screen.getCursor().getName();
		switch (GameDrawable.cursor) {
		case Cursor.HAND_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("hand cursor")) {
				GameDrawable.screen.setCursor(GameDrawable.handCursor);
			}
			break;
		case Cursor.DEFAULT_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("default cursor")) {
				GameDrawable.screen.setCursor(GameDrawable.defaultCursor);
			}
			break;
		case Cursor.WAIT_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("wait cursor")) {
				GameDrawable.screen.setCursor(GameDrawable.waitCursor);
			}
			break;
		case PAN_NORTHWEST_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan northwest")) {
				GameDrawable.screen.setCursor(GameDrawable.panNorthWest);
			}
			break;
		case PAN_NORTH_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan north")) {
				GameDrawable.screen.setCursor(GameDrawable.panNorth);
			}
			break;
		case PAN_NORTHEAST_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan northeast")) {
				GameDrawable.screen.setCursor(GameDrawable.panNorthEast);
			}
			break;
		case PAN_EAST_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan east")) {
				GameDrawable.screen.setCursor(GameDrawable.panEast);
			}
			break;
		case PAN_SOUTHEAST_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan southeast")) {
				GameDrawable.screen.setCursor(GameDrawable.panSouthEast);
			}
			break;
		case PAN_SOUTH_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan south")) {
				GameDrawable.screen.setCursor(GameDrawable.panSouth);
			}
			break;
		case PAN_SOUTHWEST_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan southwest")) {
				GameDrawable.screen.setCursor(GameDrawable.panSouthWest);
			}
			break;
		case PAN_WEST_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("pan west")) {
				GameDrawable.screen.setCursor(GameDrawable.panWest);
			}
			break;
		case ATTACK_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("attack")) {
				GameDrawable.screen.setCursor(GameDrawable.attack);
			}
			break;
		case MOVE_CURSOR:
			if (!GameDrawable.cursorName.equalsIgnoreCase("move")) {
				GameDrawable.screen.setCursor(GameDrawable.move);
			}
			break;
		default:
			break;
		}
	}
	/**
	 * Creates a new instance of GameDrawable.java.
	 * @param capabilities the {@link GLCapabilities}
	 * @param chooser the {@link GLCapabilitiesChooser}
	 * @param shareWith the {@link GLContext}
	 * @param device the {@link GraphicsDevice}
	 */
	protected GameDrawable(final GLCapabilities capabilities,
			final GLCapabilitiesChooser chooser, final GLContext shareWith,
			final GraphicsDevice device) {
		super(capabilities, chooser, shareWith, device);
		GameDrawable.screen = this;
		// Cursor cursor =
		// Toolkit.getDefaultToolkit().createCustomCursor(cursor, hotSpot,
		// name);
	}
	/** Updates the game. */
	public abstract void gameUpdate();
	/**
	 * Sets the time spent in the game.
	 * @param time the time
	 */
	public abstract void setTimeSpentInGame(final int time);
}
